<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>批量服务管理</title>
    <% include("../layout/css.html"){} %>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格顶部工具栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input name="keywords" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn" lay-filter="searchSubmit" lay-submit><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="taskInfTable" lay-filter="taskInfTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableState">
    <input type="checkbox" lay-filter="ckState" value="{{d.id}}" lay-skin="switch"
           lay-text="启动|停止" {{d.taskStat=='01'?'checked':''}}/>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelTaskInf">
    <form id="modelTaskInfForm" lay-filter="modelTaskInfForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">任务名</label>
            <div class="layui-input-block">
                <input name="taskName" placeholder="任务名" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">定时任务表达式</label>
            <div class="layui-input-block">
                <input name="taskExpress" placeholder="请输入定时任务表达式" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">任务别名</label>
            <div class="layui-input-block">
                <input name="aliasName" placeholder="请输入任务别名" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">状态</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input name="taskStat" type="radio" value="00" title="关闭" checked disabled/>-->
<!--                <input name="taskStat" type="radio" value="01" title="启动" disabled/>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">任务机器号</label>
            <div class="layui-input-block">
                <input name="machId" placeholder="请输入任务机器号" type="text" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属集群</label>
            <div class="layui-input-block">
                <input name="clusterName" placeholder="请输入所属集群" type="text" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelTaskInfSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<% include("../layout/js.html"){} %>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'tableX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var tableX = layui.tableX;

        // 渲染表格
        var insTb = tableX.render({
            elem: '#taskInfTable',
            url: 'taskInf/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'taskName', title: '任务名'},
                {field: 'taskExpress', title: '定时任务表达式'},
                {field: 'aliasName', title: '任务别名'},
                {field: 'oprId', title: '操作员id', align: 'center'},
                // {
                //     title: '状态', templet: function (d) {
                //         var strs = ['<span class="layui-badge-rim">关闭</span>', '<span class="layui-badge layui-bg-gray">开启</span>'];
                //         if(d.taskStat=='00'){
                //             return strs[0];
                //         } else {
                //             return strs[1];
                //         }
                //     }, align: 'center'
                // },
                {field: 'taskStat', templet: '#tableState', title: '状态', sort: true},
                {field: 'machId', title: '任务机器号', align: 'center'},
                {field: 'clusterName', title: '所属集群', align: 'center'},
                // {
                //     field: 'createTime', sort: true, templet: function (d) {
                //         return util.toDateString(d.createTime);
                //     }, title: '创建时间'
                // },
                // {
                //     field: 'updateTime', sort: true, templet: function (d) {
                //         return util.toDateString(d.updateTime);
                //     }, title: '更新时间'
                // },
                {templet: '#tableBar', title: '操作', align: 'center', minWidth: 120},
            ]]
        });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(taskInfTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.taskName);
            }
        });

        // 删除
        function doDel(id, taskName) {
            layer.confirm('确定要删除“' + taskName + '”吗？', {
                shade: .1,
                skin: 'layui-layer-admin'
            }, function (index) {
                layer.close(index);
                layer.load(2);
                $.post('taskInf/delete', {
                    taskInfId: id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        // 显示表单弹窗
        function showEditModel(taskInf) {
            admin.open({
                type: 1,
                title: (taskInf ? '修改' : '添加') + '批量定时任务',
                content: $('#modelTaskInf').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = taskInf ? 'taskInf/update' : 'taskInf/add';
                    if (taskInf && taskInf.taskStat == '01') {
                        $('input[name="taskStat"][value="01"]').prop("checked", true);
                    }
                    form.val('modelTaskInfForm', taskInf);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelTaskInfSubmit)', function (data) {
                        layer.load(2);
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

        // 搜索
        form.on('submit(searchSubmit)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
        });

        // 修改用户状态
        form.on('switch(ckState)', function (obj) {
            layer.load(2);
            $.post('taskInf/updateState', {
                taskInfoId: obj.elem.value,
                taskStat: obj.elem.checked ? '01' : '00'
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

    });
</script>
</body>
</html>